import pandas as pd
import numpy as np




def read_csv():
    filepath = "../datas/student_grade/学生成绩.csv"
    df = pd.read_csv(filepath)
    df["one"] = 1

    df_merge = pd.merge(left=df, right=df, left_on="one", right_on="one")
    print(df_merge.head())
    # 2. 两两计算相似度
    columns = list(df.columns)
    columns.remove("姓名")
    columns.remove("one")
    print(columns)

    # 所有的列 数据 求和
    def sim_fun(row):
        sim_value = 0.0
        for column in columns:
            sim_value += abs(int(row[column+"_x"]) - int(row[column+"_y"]))
        return sim_value
    #  数据比较
    df_merge["sim"] = df_merge.apply(sim_fun, axis=1)

    print(df_merge.head(10))

    # 3. 计算每个学生的TOP N的学生
    def get_top_student(df_sub):
        df_sort = df_sub.sort_values(by="sim", ascending=False).head(10)
        names = ",".join(list(df_sort["姓名_y"]))
        sims = ",".join([str(x) for x in list(df_sort["sim"])])
        return pd.Series({"names": names, "sims": sims})

    df_result = df_merge.groupby("姓名_x").apply(get_top_student)
    print(df_result.head())
    df_result.to_excel('./file/相似计算结果.xlsx',index=True)




if __name__ == '__main__':
    read_csv()